<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单提交示例</title>
    <script src="../js/jquery-2.1.1.min.js"></script>
</head>
<body>
    <form id="f1" action="http://www.baidu.com/s" method="post">
        <div><input name="n1" type="text" /></div>
        <div><input name="n2" type="password" /></div>
        <input type="submit" value="提交" />
    </form>
    <script>
        $(':submit').click(function(){
            $('span').remove();
            var flag = true;
            $('#f1').find('input[type="text"],input[type="password"]').each(function () {
                var v = $(this).val();

                if(v.length<=0){
                    flag = false;
                    var tag = document.createElement('span');
                    $(tag).text('必填').css('color','red');
                    $(this).after(tag);
                }
            });
            return flag;
        })
    </script>
</body>
</html>